package com.toobigtofail.posevo
{
	import net.flashpunk.Entity;
	import net.flashpunk.Graphic;
	import net.flashpunk.Mask;
	
	public class PontiacOnSteroids extends Entity
	{
		private static const FRONTL:int = 0;
		private static const FRONTR:int = 1;
		private static const BACKL:int  = 2;
		private static const BACKR:int  = 3;
		
		private var _body:PointOfSit;
		private var _wheels:Array = new Array;

		private var _prevX:Number = 0;
		private var _prevY:Number = 0;
		
		public function PontiacOnSteroids(x:Number=0, y:Number=0) {
			this.x = _prevX = x;
			this.y = _prevY = y;
			
			this.layer = 100;
			
			_body = new PointOfSit(x,y);
			
			var wheel:PointOfSpin;
			
			for (var i:int = 0; i < 4; i++) {
				var wx:Number = x;
				var wy:Number = y; 
				
				wheel = new PointOfSpin;
				
				switch (i) {
					case FRONTR: wx += wheel.width + _body.width;
						         break;
					case BACKR:  wx += wheel.width + _body.width;
								 wy += _body.height - wheel.height;
							     break;
					case BACKL:  wy += _body.height - wheel.height;
						         break;
				}
				wheel.x = wx;
				wheel.y = wy;
				
				_wheels.push(wheel);
			}
			
			wheel = _wheels[FRONTL] as PointOfSpin;
			_body.x += wheel.width;
			
			this.width = _body.width + wheel.width*2;
			this.height = _body.height;
		}
		
		override public function added():void {
			super.added();
			
			this.world.add(_body);
			this.world.addList(_wheels);
		}
		
		override public function update():void {
			super.update();
			
			var diffX:Number = x - _prevX;
			var diffY:Number = y - _prevY;
			
			if (diffX != 0 || diffY != 0) {
				for each (var w:PointOfSpin in _wheels) {
					w.x += diffX;
					w.y += diffY;
				}
				_body.x += diffX;
				_body.y += diffY;
			}
			
			_prevX = x;
			_prevY = y;
		}
	}
}